package cn.msqweb.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.msqweb.entity.Notice;
import cn.msqweb.model.Result;
import cn.msqweb.service.NoticeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 公告业务控制层
 */
@Api(tags = "公告接口管理")
@RequestMapping("shop/notice")
@RestController
public class NoticeController {

    @Autowired
    private NoticeService noticeService;


    @ApiOperation("多条件分页查询公告")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('shop:notice:page')")
    public Result<Page<Notice>> loadNoticePage(@ApiParam("页码") @RequestParam Long current,
                                               @ApiParam("每页显示条数") @RequestParam Long size,
                                               @ApiParam("状态") @RequestParam(required = false) Integer status,
                                               @ApiParam("是否置顶") @RequestParam(required = false) Integer isTop,
                                               @ApiParam("公告标题") @RequestParam(required = false) String title) {
        Page<Notice> page = new Page<>(current,size);
        page = noticeService.page(page,new LambdaQueryWrapper<Notice>()
                        .eq(ObjectUtil.isNotNull(status),Notice::getStatus,status)
                        .eq(ObjectUtil.isNotNull(isTop),Notice::getIsTop,isTop)
                        .like(StringUtils.hasText(title),Notice::getTitle,title)
                .orderByDesc(Notice::getCreateTime)
        );
        return Result.success(page);
    }

    @ApiOperation("新增公告")
    @PostMapping
    @PreAuthorize("hasAuthority('shop:notice:save')")
    public Result<String> saveNotice(@ApiParam("公告对象") @RequestBody Notice notice) {
        boolean saved = noticeService.save(notice);
        return Result.handler(saved);
    }

    @ApiOperation("根据标识查询公告信息")
    @GetMapping("info/{noticeId}")
    @PreAuthorize("hasAuthority('shop:notice:info')")
    public Result<Notice> loadNoticeInfo(@ApiParam("公告标识") @PathVariable Long noticeId) {
        Notice notice = noticeService.getById(noticeId);
        return Result.success(notice);
    }

    @ApiOperation("修改公告信息")
    @PutMapping
    @PreAuthorize("hasAuthority('shop:notice:update')")
    public Result<String> modifyNoticeInfo(@ApiParam("公告对象") @RequestBody Notice notice) {
        boolean updated = noticeService.updateById(notice);
        return Result.handler(updated);
    }

    @ApiOperation("删除公告")
    @DeleteMapping("{noticeId}")
    @PreAuthorize("hasAuthority('shop:notice:delete')")
    public Result<String> removeNotice(@ApiParam("公告标识") @PathVariable Long noticeId) {
        boolean removed = noticeService.removeById(noticeId);
        return Result.handler(removed);
    }
}
